<?php

namespace front\modules\app\controllers;

use Yii;
use front\models\CollectionModel;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use goods\services\CollectionService;
use goods\services\ProductService;

/**
 * CollectionController implements the CRUD actions for CollectionModel model.
 */
class CollectionController extends WebBaseController
{
    public $enableCsrfValidation = false;
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all CollectionModel models.
     * @return mixed
     */
    public function actionIndex()
    {

    }

    /**
     * 取消收藏
     * 收藏id
     */

        public function actionRemove()
    {
        $id = yii::$app->request->post('id');
        if(!$id){
            WebBaseController::getAjaxData(2,'该收藏不存在');
        }
        $info = CollectionService::remove($id);
        if($info['code']==1){
            WebBaseController::getAjaxData(3,'删除失败');
        }
            WebBaseController::getAjaxData(1,$info['message'],$info['result']);

    }

    /**
     * 添加商品收藏
        $params=[
        'C_Id'=>20,
        'S_Id'=>1,
        'P_Id'=>20,
        'p_name'=>'ewqe',
        'pic'=>'http://www.baidu.com',
        'attributes'=>[
        'color' => '红色',
        'size' => '大',
        'price' => '255'
        ],
        ];
     */
    function actionAdd(){
        $params = yii::$app->request->post();
        $info = CollectionService::addCollection($params);
        if($info['code']==1){
            WebBaseController::getAjaxData(2,'参数错误');
        }
        WebBaseController::getAjaxData(1,$info['message'],$info['result']);
    }

    /**
     * 获取用户收藏的商品列表
     * C_Id	Integer C用户ID
     *
     *
     */

    function actionList(){
        $data = yii::$app->request->get();
        $info = CollectionService::getCollection($data);
        $arr = array_map(function($v){
            $v['attributes'] = unserialize($v['attributes']);;
            return $v;
        },$info['result']);
        if($info['code']==1){
            WebBaseController::getAjaxData(3,'操作错误');
        }
        WebBaseController::getAjaxData(1,'获取成功',$arr);
    }



    /**
     * Displays a single CollectionModel model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CollectionModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CollectionModel();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->ID]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing CollectionModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->ID]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing CollectionModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CollectionModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return CollectionModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CollectionModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
